from django.http import JsonResponse
from rest_framework import status
from rest_framework.response import Response
from rest_framework.views import APIView
from apps.movie.models import Movie
from apps.user.serializers.movie import MovieDetailInfoModelSerializer


class GetIndexMovieInfo(APIView):
    authentication_classes = []

    # permission_classes = [AllowAny]

    def get(self, request):
        all_now_movie_list = Movie.objects.filter(movie_status=1)
        all_about_movie_list = Movie.objects.filter(movie_status=2)
        all_canon_movie_list = Movie.objects.filter(movie_status=3)

        now_movie_list = all_now_movie_list[0:8]
        about_movie_list = all_about_movie_list[0:8]
        canon_movie_list = all_canon_movie_list[0:8]

        now_count = len(all_now_movie_list)
        about_count = len(all_about_movie_list)
        canon_count = len(all_canon_movie_list)

        new_now_movie_list = []
        new_about_movie_list = []
        new_canon_movie_list = []
        for now_movie in now_movie_list:
            new_now_movie_list.append({
                'id': now_movie.id,
                'name': now_movie.name,
                'start': now_movie.start,
                'de_url': '/movie_detail.html?m_id=' + str(now_movie.id)
            })

        for about_movie in about_movie_list:
            new_about_movie_list.append({
                'id': about_movie.id,
                'name': about_movie.name,
                'start': about_movie.start,
                'de_url': '/movie_detail.html?m_id=' + str(about_movie.id)
            })
        for canon_movie in canon_movie_list:
            new_canon_movie_list.append({
                'id': canon_movie.id,
                'name': canon_movie.name,
                'start': canon_movie.start,
                'de_url': '/movie_detail.html?m_id=' + str(canon_movie.id)
            })

        return JsonResponse({
            'code': 0,
            'errmsg': 'ok',
            'new_now_movie_list': new_now_movie_list,
            'new_about_movie_list': new_about_movie_list,
            'now_count': now_count,
            'about_count': about_count,
            'new_canon_movie_list': new_canon_movie_list,
            'canon_count': canon_count,
        })


class GetMovieDetailInfo(APIView):
    authentication_classes = []

    def get(self, request, m_id):
        try:
            movie_info = Movie.objects.get(id=m_id)
        except Exception as e:

            print(e)
            return JsonResponse({'code': 404, 'errmsg': '找不到页面'})
        movieSerializer = MovieDetailInfoModelSerializer(movie_info)
        return JsonResponse({'code': 0, 'movie_info': movieSerializer.data})
